/**
 * 
 */
package com.java.test.platmanage.service.impl;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java.test.mapper.AreaMapper;
import com.java.test.platmanage.dto.GameListInfo;
import com.java.test.platmanage.dto.PlatParamInfo;
import com.java.test.platmanage.mapper.GameListMapper;
import com.java.test.platmanage.mapper.PlatParamMapper;
import com.java.test.platmanage.service.PlatService;
import com.java.test.system.registrationtemplate.entity.AreaEntity;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@Service
public class PPlatServiceImpl implements PlatService {

	@Autowired
	PlatParamMapper platParamMapper;

	@Autowired
	GameListMapper gameListMapper;

	@Autowired
	AreaMapper areaMapper;
	
	
	@Autowired
    private com.java.test.system.fundstypemanage.FundsTypeDAO fundsTypeDAO;

	@Override
	public List<PlatParamInfo> selectplatList() {
		return platParamMapper.selectplatList();
	}

	@Override
	public int insertplatParam(PlatParamInfo info) {
		return platParamMapper.insertplatParam(info);
	}

	@Override
	public int updateplatParam(PlatParamInfo info) {
		return platParamMapper.updateplatParam(info);
	}

	@Override
	public PageInfo<GameListInfo> selectgameList(PageInfo<GameListInfo> page) {

		GameListInfo info = page.getInfo();

		int total = gameListMapper.selectgameListcount(info);

		List<GameListInfo> list = gameListMapper.selectgameList(info, page);
		page.setTotalCount(total);

		
		
		
		list.forEach(b -> {
			
			JSONArray jsobj  = JSONArray.parseArray(b.getOpenLanguage());	
			String langstr="";
			for(int i=0;i<jsobj.size();i++) {
				langstr+=jsobj.get(i)+",";
			}
			langstr=langstr.substring(0, langstr.length()-1);
			b.setOpenLanguage(langstr);


			
			String areaids = b.getAreaId();

			if (areaids != null && !"".equals(areaids)) {
				List<AreaEntity> arealist = areaMapper.selectAreaNamebyId(areaids);

				if (arealist != null && arealist.size() > 0) {

					String areaName = "";

					for (AreaEntity c : arealist) {
						areaName += c.getName() + ",";
					}
					areaName = areaName.substring(0, areaName.length() - 1);

					b.setAreaName(areaName);

				}
			}

			// 设置平台名称
			int platId = b.getPlatId();

			PlatParamInfo pinfo = platParamMapper.selectplatInfoById(platId);

			if (pinfo == null)
				return;
			b.setPlatName(pinfo.getPlatName());

		});

		page.setList(list);

		return page;
	}

	@Override
	public GameListInfo selectgamebyId(int id) {

		GameListInfo b = gameListMapper.selectgamebyId(id);
		
		JSONArray jsobj  = JSONArray.parseArray(b.getOpenLanguage());	
		String langstr="";
		for(int i=0;i<jsobj.size();i++) {
			langstr+=jsobj.get(i)+",";
		}
		langstr=langstr.substring(0, langstr.length()-1);
		b.setOpenLanguage(langstr);

		// 设置平台名称
		int platId = b.getPlatId();

		if (platId == 0)
			return b;

		PlatParamInfo pinfo = platParamMapper.selectplatInfoById(platId);

		if (pinfo == null)
			return b;

		b.setPlatName(pinfo.getPlatName());

		return b;
	}

	@Override
	public int updateGameListInfo(GameListInfo info) {
		return gameListMapper.updateGameListInfo(info);
	}

	@Override
	public int updateGamesortInfo(List<GameListInfo> info) {
		return gameListMapper.updateGamesortInfo(info);
	}

	@Override
	public int insertGameInfo(GameListInfo info) {
		
		return gameListMapper.insertGameInfo(info);
	}

	
	@Override
	public List<String> selectallGame() {
		return gameListMapper.selectallGame();
	}

	
	
	@Override
	public int updategamestatusInfo(GameListInfo info) {
		return gameListMapper.updategamestatusInfo(info);
	}
	
	@Override
	public int updategamestatusInfoPL(int[] ids,int status) {
		return gameListMapper.updategamestatusInfoPL(ids,status);
	}
	
	@Override
	public int updategameisnothotInfoPL(int[] ids,int isnotHot) {
		return gameListMapper.updategameisnothotInfoPL(ids,isnotHot);
	}
	
	
	
	
	

	@Override
	public int updateplatparamStatus(PlatParamInfo info) {
		return platParamMapper.updateplatparamStatus(info);
	}

	@Override
	public boolean selectcheckdomain(String domainName) {
		return platParamMapper.selectcheckdomain(domainName);
	}

	@Override
	public boolean selectplatname(String platName) {
		return platParamMapper.selectplatname(platName);
	}

	@Override
	public List<GameListInfo> selectgamebygametype(int gametypeid) {
		return gameListMapper.selectgamebygametype(gametypeid);
	}

}
